package com.fh.controller.hydropower;

import com.fh.controller.base.BaseController;
import com.fh.entity.Page;
import com.fh.entity.system.User;
import com.fh.service.building.bill.BillService;
import com.fh.service.hydropower.HydropowerDetailService;
import com.fh.service.hydropower.HydropowerService;
import com.fh.util.CodeConstants;
import com.fh.util.DateUtil;
import com.fh.util.PageData;
import com.fh.util.StringHelper;
import net.sf.json.JSONArray;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import java.io.PrintWriter;
import java.math.BigDecimal;
import java.util.*;

import static com.fh.controller.system.tools.GetDatalayui.getLData;
import static com.fh.controller.system.tools.GetDatalayui.getPData;

/**
 * 类名称：Hydropower_detailController
 * 创建人：FH
 * 创建时间：2017-10-04
 */
@Controller
@RequestMapping(value = "/hydropowerdetail")
public class HydropowerDetailController extends BaseController {

    @Resource(name = "hydropowerDetailService")
    private HydropowerDetailService hydropowerDetailService;
    @Resource(name = "hydropowerService")
    private HydropowerService hydropowerService;
    @Resource(name = "billService")
    private BillService billService;

    /**
     * 新增
     */
    @RequestMapping(value = "/save")
    public ModelAndView save() throws Exception {
        logBefore(logger, "新增Hydropower_detail");
        ModelAndView mv = this.getModelAndView();
        PageData pd = new PageData();
        pd = this.getPageData();
        pd.put("ID", this.get32UUID());    //主键
        hydropowerDetailService.save(pd);
        mv.addObject("msg", "success");
        mv.setViewName("save_result");
        return mv;
    }

    /**
     * 删除
     */
    @RequestMapping(value = "/delete")
    public void delete(PrintWriter out) {
        logBefore(logger, "删除Hydropower_detail");
        PageData pd = new PageData();
        try {
            pd = this.getPageData();
            hydropowerDetailService.delete(pd);
            out.write("success");
            out.close();
        } catch (Exception e) {
            logger.error(e.toString(), e);
        }

    }

    /**
     * 弃用 修改  生成抄表记录
     */
    @RequestMapping(value = "/edit_old")
    @ResponseBody
    public PageData edit_old() throws Exception {
        logBefore(logger, "修改Hydropower_detail");
        PageData resultPd = new PageData();
        PageData pd = new PageData();
        try {
            pd = this.getPageData();
            //取搜索条件 《月》 的表记录 如果存在记录 就修改 如果不存在记录 就插入新记录
            //fixme 有当月电表记录ID 如果修改了上个月的底数  这个月的上次抄表数 暂没处理  old—弃用
            //fixme 每次都生成一次抄表记录 2018年5月17日 10:31:16
            if (pd.containsKey("id") && StringHelper.isNotEmpty(pd.getString("id"))) {
                hydropowerDetailService.edit(pd);
            } else {
                //无当月电表记录
                String curDate = pd.getString("curDate");
                int year = 0;
                int month = 0;
                if (StringHelper.isNotEmpty(curDate)) {
                    String[] dates = curDate.split("-");
                    year = Integer.parseInt(dates[0]);
                    month = Integer.parseInt(dates[1]);
                } else {
                    Calendar calendar = Calendar.getInstance();
                    year = calendar.get(Calendar.YEAR);
//					month = calendar.get(Calendar.MONTH)+1;
                    month = Integer.parseInt(pd.get("req_month").toString().substring(5, 7));//没有记录的时候 存取到的当前月份 2018年4月25日 18:23:05
                }
                PageData where = new PageData();
                where.put("code", pd.getString("eid"));
                where.put("year", year);
                where.put("month", month);
                where = hydropowerDetailService.findByCode(where);
                if (where != null) {
                    pd.put("id", where.getString("ID"));
                    hydropowerDetailService.edit(pd);
                } else {
                    //当切换到不是当前月的时候 把切换到的日期的前一个的数据 更新到选择的当前月 2018年4月25日 18:19:28曹增令
                    PageData power = new PageData();
                    where = new PageData();
                    where.put("code", pd.getString("eid"));
                    where.put("year", year);
                    int last_month = Integer.parseInt(pd.get("req_month").toString().substring(5, 7)) - 1;
                    where.put("month", last_month);
                    where = hydropowerDetailService.findByCode(where);
                    if (where != null) {
                        //有上月抄表记录
                        power.put("lastDegrees", where.get("CUR_DEGREES"));
                        power.put("lastDate", where.getString("CUR_DATE"));
                    } else {
                        //无上月抄表记录
//                        power.put("lastDegrees", 0);
//                        power.put("lastDate", null);
                        /*
                         * 	author：lirui
                         *   date：2018年5月8日15:38:56
                         *   desc：更新本次读数时将上一次的读数记录放到本次记录的上次读数字段里
                         * */
                        PageData lastDegrees = hydropowerDetailService.findLastDegreesByCode(pd);
                        if (lastDegrees == null) {
                            power.put("lastDegrees", 0);
                            power.put("lastDate", "");
                        } else {
                            power.put("lastDegrees", lastDegrees.get("CUR_DEGREES"));
                            power.put("lastDate", lastDegrees.get("CUR_DATE"));
                        }
                    }
                    power.put("createId", this.getSessionUser().getUSER_ID());
                    power.put("createDate", DateUtil.getTime());
                    power.put("sort", 0);
                    power.put("isDel", 0);
                    power.put("code", pd.getString("eid"));
                    power.put("curDegrees", pd.getString("curDegrees"));
                    power.put("curDate", pd.getString("curDate"));
                    power.put("isBill", 0);
                    power.put("id", this.get32UUID());
                    hydropowerDetailService.save(power);
                }

            }
            resultPd.put("errcode", "00");
            resultPd.put("errmsg", "操作成功");
        } catch (Exception e) {
            resultPd.put("errcode", "01");
            resultPd.put("errmsg", "操作失败");
        }
        return resultPd;
    }

    /**
     * 修改  生成抄表记录  本地读数一定要大于上次读数
     */
    @RequestMapping(value = "/edit_info")
    @ResponseBody
    public PageData edit_info() throws Exception {
        logBefore(logger, "修改Hydropower_detail");
        PageData resultPd = new PageData();
        PageData pd = new PageData();
        try {
            pd = this.getPageData();
            hydropowerDetailService.edit(pd);
            resultPd.put("errcode", "00");
            resultPd.put("errmsg", "操作成功");
        } catch (Exception e) {
            resultPd.put("errcode", "01");
            resultPd.put("errmsg", "操作失败");
        }
        return resultPd;
    }

    /**
     * 新增 新的 生成抄表记录
     */
    @RequestMapping(value = "/edit")
    @ResponseBody
    public PageData edit() throws Exception {
        logBefore(logger, "修改Hydropower_detail");
        PageData resultPd = new PageData();
        PageData pd = new PageData();
        try {
            pd = this.getPageData();
            //fixme 每次都生成一次抄表记录 2018年5月17日 10:31:16
            //无当月电表记录
            String curDate = pd.getString("curDate");
            int year = 0;
            int month = 0;
            if (StringHelper.isNotEmpty(curDate)) {
                String[] dates = curDate.split("-");
                year = Integer.parseInt(dates[0]);
                month = Integer.parseInt(dates[1]);
            } else {
                Calendar calendar = Calendar.getInstance();
                year = calendar.get(Calendar.YEAR);
                month = Integer.parseInt(pd.get("req_month").toString().substring(5, 7));//没有记录的时候 存取到的当前月份 2018年4月25日 18:23:05
            }
            //抄表度数 不能小于上一次
            List<PageData> Detail_last =hydropowerDetailService.findHyDetail_last(pd);
            //查询水电表
            if(Detail_last.size()>0){
                if(Double.parseDouble(Detail_last.get(0).get("CUR_DEGREES").toString())>Double.parseDouble(pd.get("curDegrees").toString())){
                    resultPd.put("errcode", "00");
                    resultPd.put("errmsg", "当前抄表数不能小于上次抄表数");
                }else if(DateUtil.compareDate(Detail_last.get(0).get("CUR_DATE").toString(),pd.get("curDate").toString())){
                    resultPd.put("errcode", "00");
                    resultPd.put("errmsg", "当前抄表日期不能小于上次抄表日期");
                }else {
                    PageData power = new PageData();
                    power.put("createId", this.getSessionUser().getUSER_ID());
                    power.put("createDate", DateUtil.getTime());
                    power.put("sort", 0);
                    power.put("isDel", 0);
                    power.put("code", pd.getString("eid"));
                    power.put("curDegrees", pd.getString("curDegrees"));
                    power.put("curDate", pd.getString("curDate"));
                    power.put("isBill", 0);
                    power.put("id", this.get32UUID());
                    hydropowerDetailService.save(power);
                    resultPd.put("errcode", "00");
                    resultPd.put("errmsg", "当前读数录入成功");
                }
            }
        } catch (Exception e) {
            resultPd.put("errcode", "01");
            resultPd.put("errmsg", "操作失败");
        }
        return resultPd;
    }

    /**
     * 列表
     */
    @RequestMapping(value = "/list")
    @ResponseBody
    public Object list(Page page) {
        logBefore(logger, "列表Hydropower_detail");
        PageData pd = new PageData();
        String datalayui = null;
        User user = this.getSessionUser();
        try {
            pd = this.getPageData();
            pd.put("USER_ID", user.getUSER_ID());
            String time = pd.getString("time");
            int year = 0;
            int month = 0;
            if (StringHelper.isNotEmpty(time)) {
                String[] dates = time.split("-");
                year = Integer.parseInt(dates[0]);
                month = Integer.parseInt(dates[1]);
            }
            pd.put("year", year);
            pd.put("month", month);
            pd.put("USER_ID", user.getUSER_ID());
            getPData(page, pd);
            page.setPd(pd);
            List<PageData> varList = hydropowerDetailService.list(page);    //列出Hydropower_detail列表
            datalayui = getLData(page, varList);//必填 2参
        } catch (Exception e) {
            logger.error(e.toString(), e);
        }
        return datalayui;
    }


    /**
     * 生成当月抄表记录
     */
    @RequestMapping(value = "/genrocord")
    @ResponseBody
    public Object genRecord(Page page) {
        logBefore(logger, "列表Hydropower_detail");
        PageData pd = new PageData();
        PageData resultPd = new PageData();
        try {
            pd = this.getPageData();
            pd.put("time", DateUtil.getLastMonthDate());
            List<PageData> oldList = hydropowerDetailService.listByBuildingId(pd);
            List<PageData> newList = new ArrayList<PageData>();
            if (oldList.size() > 0) {
                for (int i = 0; i < oldList.size(); i++) {
                    PageData oldPower = oldList.get(i);
                    PageData power = new PageData();
                    power.put("createId", this.getSessionUser().getUSER_ID());
                    power.put("createDate", DateUtil.getTime());
                    power.put("sort", 0);
                    power.put("isDel", 0);
                    power.put("code", oldPower.getString("ID"));
                    power.put("lastDegrees", oldPower.get("CUR_DEGREES") == null ? 0 : oldPower.get("CUR_DEGREES"));
                    power.put("lastDate", oldPower.get("CUR_DATE"));
                    power.put("curDegrees", 0);
                    power.put("curDate", "");
                    power.put("isBill", 0);
                    power.put("id", this.get32UUID());
                    newList.add(power);
                }
            }
            hydropowerDetailService.insertByBatch(newList);
            resultPd.put("errcode", "00");
            resultPd.put("errmsg", "操作成功");
        } catch (Exception e) {
            resultPd.put("errcode", "01");
            resultPd.put("errmsg", "操作失败");
            logger.error(e.toString(), e);
        }
        return resultPd;
    }

    /**
     * 生成账单
     *
     * @return
     */
    @RequestMapping(value = "/genbill")
    @ResponseBody
    public Object genBill() {
        logBefore(logger, "列表Hydropower_detail");
        PageData pd = new PageData();
        PageData resultPd = new PageData();
        try {
            pd = this.getPageData();
            String billType = "";
            if (pd.getString("type").equals(CodeConstants.HYDROPOWER_ELECTRIC)) {
//				pd.put("propertyId",CodeConstants.PROPERTY_ELECTRIC);
                billType = CodeConstants.PROPERTY_ELECTRIC;
            } else {
//				pd.put("propertyId",CodeConstants.PROPERTY_WATER);
                billType = CodeConstants.PROPERTY_WATER;
            }
            pd.put("state", CodeConstants.CONTRACT_UNDER);
            pd.put("time", DateUtil.getDay());
            //查询有效合同信息
            PageData contractInfo = hydropowerDetailService.findContractInfo(pd);
            //查询电表信息
            PageData hydroInfo = hydropowerDetailService.findHydroInfo(pd);
            double lastDegrees = Double.valueOf(String.valueOf(hydroInfo.get("LAST_DEGREES")));//上次度数
            double curDegrees = Double.valueOf(String.valueOf(hydroInfo.get("CUR_DEGREES")));//本次度数
            double countDegrees = 0;//度数差
            double rates = Double.valueOf(String.valueOf(hydroInfo.get("RATES")));//电表倍率
            double totalPrice = 0;
            double chargePrice = 0;
            String contractId = null;
            String propertyId = null;
            String hydropowerId = hydroInfo.getString("HYDRO_ID");
            String days = "0";
            String personId = null;
            String errcode = "";
            if (billType.equals(CodeConstants.PROPERTY_ELECTRIC)) {
                chargePrice = Double.valueOf(String.valueOf(hydroInfo.get("ELETRIC_PRICE")));
            } else {
                chargePrice = Double.valueOf(String.valueOf(hydroInfo.get("WATER_PRICE")));
            }
            if (contractInfo == null) {
                countDegrees = curDegrees - lastDegrees;
                if (pd.getString("type").equals(CodeConstants.HYDROPOWER_ELECTRIC)) {
                    countDegrees *= rates;//电表乘以倍率
                }
                totalPrice = countDegrees * chargePrice;
            } else {
                //收费方式 固定价格无需生成账单
                if (StringHelper.isNotEmpty(contractInfo.getString("CHARGE_TYPE")) && contractInfo.getString("CHARGE_TYPE").equals(CodeConstants.CHARGE_FIXED)) {
                    errcode = "01";
                } else if (StringHelper.isNotEmpty(contractInfo.getString("CHARGE_TYPE")) && contractInfo.getString("CHARGE_TYPE").equals(CodeConstants.CHARGE_METER)) {
                    contractId = contractInfo.getString("CONTRACT_ID");
                    personId = contractInfo.getString("PERSON_ID");
                    days = contractInfo.getString("CHARGE_DATE");
                    propertyId = contractInfo.getString("CONTRACT_PROPERTY_ID");
                    double firstDegrees = Double.valueOf(String.valueOf(contractInfo.get("INIT_READ")));//合同记录度数
                    if (String.valueOf(hydroInfo.get("IS_NEW")).equals("1")) {
                        countDegrees = curDegrees - firstDegrees;//首次账单减合同度数
                        PageData hydro = new PageData();
                        hydro.put("id", hydroInfo.getString("HYDRO_ID"));
                        hydro.put("isNew", 0);
                        hydropowerService.edit(hydro);
                    } else if (String.valueOf(hydroInfo.get("IS_NEW")).equals("0")) {
                        countDegrees = curDegrees - lastDegrees;//非首次账单减上月度数
                    }
                    if (pd.getString("type").equals(CodeConstants.HYDROPOWER_ELECTRIC)) {
                        countDegrees *= rates;//电表乘以倍率
                    }
                    //计价类型
                    if (StringHelper.isNotEmpty(contractInfo.getString("VALUATION_TYPE")) && contractInfo.getString("VALUATION_TYPE").equals(CodeConstants.VALUATION_STEP)) {
                        //阶梯计价
                        pd.put("contractPropertyId", contractInfo.getString("CONTRACT_PROPERTY_ID"));
                        List<PageData> tired = hydropowerDetailService.findContractTired(pd);
                        totalPrice = hydropowerDetailService.getTiredPrice(countDegrees, tired);
                    } else {
                        //正常计价
                        if (String.valueOf(contractInfo.get("IS_DEFAULT")).equals("0")) {
                            chargePrice = Double.valueOf(String.valueOf(contractInfo.get("CHARGE_PRICE")));//合同费用价格
                        }
                        totalPrice = countDegrees * chargePrice;
                    }
                    errcode = "00";
                }
            }
            PageData bill = billService.getBill(this.get32UUID(), this.getSessionUser().getUSER_ID(), contractId, totalPrice, billType, days, personId, hydropowerId, String.valueOf(chargePrice));
            billService.save(bill);
            hydroInfo.put("id", hydroInfo.getString("DETAIL_ID"));
            hydroInfo.put("isBill", 1);
            hydropowerDetailService.edit(hydroInfo);
            if (errcode.equals("01")) {
                resultPd.put("errcode", "01");
                resultPd.put("errmsg", "该项为固定价格收费，无需生成账单");
            } else {
                resultPd.put("errcode", "00");
                resultPd.put("errmsg", "操作成功");
            }
        } catch (Exception e) {
            resultPd.put("errcode", "02");
            resultPd.put("errmsg", "操作失败");
            logger.error(e.toString(), e);
        }
        return resultPd;
    }


    /**
     * 批量生成账单
     *
     * @return
     */
    @RequestMapping(value = "/genbillmany")
    @ResponseBody
    public Object genBillMany() {
        logBefore(logger, "列表Hydropower_detail");
        PageData pd = new PageData();
        PageData resultPd = new PageData();
        try {
            pd = this.getPageData();
            String data = pd.getString("data");
            String[] ds = data.split("#");
            if (ds.length > 0) {
                for (String info : ds) {
                    String[] is = info.split(",");
                    String eid = is[0];
                    String type = is[1];
                    String billType = "";
                    if (type.equals(CodeConstants.HYDROPOWER_ELECTRIC)) {
                        pd.put("propertyId", CodeConstants.PROPERTY_ELECTRIC);
                        billType = CodeConstants.PROPERTY_ELECTRIC;
                    } else {
                        pd.put("propertyId", CodeConstants.PROPERTY_WATER);
                        billType = CodeConstants.PROPERTY_WATER;
                    }
                    pd.put("state", CodeConstants.CONTRACT_UNDER);
                    pd.put("time", DateUtil.getDay());
                    pd.put("eid", eid);
                    pd.put("type", type);
                    //查询有效合同信息
                    PageData contractInfo = hydropowerDetailService.findContractInfo(pd);
                    //查询电表信息
                    PageData hydroInfo = hydropowerDetailService.findHydroInfo(pd);
                    double lastDegrees = Double.valueOf(String.valueOf(hydroInfo.get("LAST_DEGREES")));//上次度数
                    double curDegrees = Double.valueOf(String.valueOf(hydroInfo.get("CUR_DEGREES")));//本次度数
                    double countDegrees = 0;//度数差
                    double rates = Double.valueOf(String.valueOf(hydroInfo.get("RATES")));//电表倍率
                    double totalPrice = 0;
                    double chargePrice = 0;
                    String contractId = null;
                    String propertyId = null;
                    String hydropowerId = hydroInfo.getString("HYDRO_ID");
                    String days = "0";
                    String personId = null;
                    if (billType.equals(CodeConstants.PROPERTY_ELECTRIC)) {
                        chargePrice = Double.valueOf(String.valueOf(hydroInfo.get("ELETRIC_PRICE")));
                    } else {
                        chargePrice = Double.valueOf(String.valueOf(hydroInfo.get("WATER_PRICE")));
                    }
                    if (contractInfo == null) {
                        countDegrees = curDegrees - lastDegrees;
                        if (pd.getString("type").equals(CodeConstants.HYDROPOWER_ELECTRIC)) {
                            countDegrees *= rates;//电表乘以倍率
                        }
                        totalPrice = countDegrees * chargePrice;
                    } else {
                        //收费方式 固定价格无需生成账单
                        if (StringHelper.isNotEmpty(contractInfo.getString("CHARGE_TYPE")) && contractInfo.getString("CHARGE_TYPE").equals(CodeConstants.CHARGE_FIXED)) {
                        } else if (StringHelper.isNotEmpty(contractInfo.getString("CHARGE_TYPE")) && contractInfo.getString("CHARGE_TYPE").equals(CodeConstants.CHARGE_METER)) {
                            contractId = contractInfo.getString("CONTRACT_ID");
                            personId = contractInfo.getString("PERSON_ID");
                            days = contractInfo.getString("CHARGE_DATE");
                            propertyId = contractInfo.getString("CONTRACT_PROPERTY_ID");
                            double firstDegrees = Double.valueOf(String.valueOf(contractInfo.get("INIT_READ")));//合同记录度数
                            if (String.valueOf(hydroInfo.get("IS_NEW")).equals("1")) {
                                countDegrees = curDegrees - firstDegrees;//首次账单减合同度数
                                PageData hydro = new PageData();
                                hydro.put("id", hydroInfo.getString("HYDRO_ID"));
                                hydro.put("isNew", 0);
                                hydropowerService.edit(hydro);
                            } else if (String.valueOf(hydroInfo.get("IS_NEW")).equals("0")) {
                                countDegrees = curDegrees - lastDegrees;//非首次账单减上月度数
                            }
                            if (pd.getString("type").equals(CodeConstants.HYDROPOWER_ELECTRIC)) {
                                countDegrees *= rates;//电表乘以倍率
                            }
                            //计价类型
                            if (StringHelper.isNotEmpty(contractInfo.getString("VALUATION_TYPE")) && contractInfo.getString("VALUATION_TYPE").equals(CodeConstants.VALUATION_STEP)) {
                                //阶梯计价
                                pd.put("contractPropertyId", contractInfo.getString("CONTRACT_PROPERTY_ID"));
                                List<PageData> tired = hydropowerDetailService.findContractTired(pd);
                                totalPrice = hydropowerDetailService.getTiredPrice(countDegrees, tired);
                            } else {
                                //正常计价
                                if (String.valueOf(contractInfo.get("IS_DEFAULT")).equals("0")) {
                                    chargePrice = Double.valueOf(String.valueOf(contractInfo.get("CHARGE_PRICE")));//合同费用价格
                                }
                                totalPrice = countDegrees * chargePrice;
                            }
                        }
                    }
                    PageData bill = billService.getBill(this.get32UUID(), this.getSessionUser().getUSER_ID(), contractId, totalPrice, billType, days, personId, hydropowerId, String.valueOf(chargePrice));
                    billService.save(bill);
                    hydroInfo.put("id", hydroInfo.getString("DETAIL_ID"));
                    hydroInfo.put("isBill", 1);
                    hydropowerDetailService.edit(hydroInfo);
                }
            }
            resultPd.put("errcode", "00");
            resultPd.put("errmsg", "操作成功");
        } catch (Exception e) {
            resultPd.put("errcode", "01");
            resultPd.put("errmsg", "操作失败");
            logger.error(e.toString(), e);
        }
        return resultPd;
    }

    /**
     * 去新增页面
     */
    @RequestMapping(value = "/goAdd")
    public ModelAndView goAdd() {
        logBefore(logger, "去新增Hydropower_detail页面");
        ModelAndView mv = this.getModelAndView();
        PageData pd = new PageData();
        pd = this.getPageData();
        try {
            mv.setViewName("hydropower/hydropower_detail/hydropower_detail_edit");
            mv.addObject("msg", "save");
            mv.addObject("pd", pd);
        } catch (Exception e) {
            logger.error(e.toString(), e);
        }
        return mv;
    }

    /**
     * 去修改页面
     */
    @RequestMapping(value = "/goEdit")
    public ModelAndView goEdit() {
        logBefore(logger, "去修改Hydropower_detail页面");
        ModelAndView mv = this.getModelAndView();
        PageData pd = new PageData();
        pd = this.getPageData();
        try {
            pd = hydropowerDetailService.findById(pd);    //根据ID读取
            mv.setViewName("hydropower/hydropower_detail/hydropower_detail_edit");
            mv.addObject("msg", "edit");
            mv.addObject("pd", pd);
        } catch (Exception e) {
            logger.error(e.toString(), e);
        }
        return mv;
    }


    /**
     * 生成账单新
     *
     * @return
     */
    @RequestMapping(value = "/genbillnew")
    @ResponseBody
    public Object genBillNew() {
        logBefore(logger, "生成账单");
        PageData pd = new PageData();
        PageData resultPd = new PageData();
        String errcode = "00";
        try {
            pd = this.getPageData();
            String billType = "";
            if (pd.getString("type").equals(CodeConstants.HYDROPOWER_ELECTRIC)) {
                billType = CodeConstants.PROPERTY_ELECTRIC;
            } else {
                billType = CodeConstants.PROPERTY_WATER;
            }
            pd.put("state", CodeConstants.CONTRACT_UNDER);
            String time = pd.getString("time");
            int year = 0;
            int month = 0;
            double totalPrice = 0;//水电总费用
            double unitPrice = 0;//水电总价
            double totalDegrees = 0;//水电总度数
            double lastDegrees = 0;//上月底数
            double curDegrees = 0;//本月度数
            double rates = 0;//电表倍率
            double proportion = 0;//发票占比
            PageData contractInfo = new PageData();
            PageData hydroInfo = new PageData();
            String state = CodeConstants.PAYMENT_WAIT;
            String hID = null;
            if (StringHelper.isNotEmpty(time)) {
                String[] dates = time.split("-");
                year = Integer.parseInt(dates[0]);
                month = Integer.parseInt(dates[1]);
                pd.put("year", year);
                pd.put("month", month);
                //查询有效合同信息
                contractInfo = hydropowerDetailService.findContractDatas(pd);
                //查询电表信息
                hydroInfo = hydropowerDetailService.findHydroInfo(pd);
                hID = hydroInfo.getString("HYDRO_ID");
                if (hydroInfo.containsKey("IS_CARD") && String.valueOf(hydroInfo.get("IS_CARD")).equals("1")) {
                    state = CodeConstants.PAYMENT_END;
                }

                if (hydroInfo.containsKey("LAST_DEGREES") && StringHelper.isNotEmpty(String.valueOf(hydroInfo.get("LAST_DEGREES")))) {
                    lastDegrees = Double.valueOf(String.valueOf(hydroInfo.get("LAST_DEGREES")));
                } else {
                    errcode = "01";//该水电表上次读数为空
                }
                if (hydroInfo.containsKey("CUR_DEGREES") && StringHelper.isNotEmpty(String.valueOf(hydroInfo.get("CUR_DEGREES")))) {
                    curDegrees = Double.valueOf(String.valueOf(hydroInfo.get("CUR_DEGREES")));
                } else {
                    errcode = "02";//该水电表本次读数为空
                }
                if (hydroInfo.containsKey("RATES") && StringHelper.isNotEmpty(String.valueOf(hydroInfo.get("RATES")))) {
                    rates = Double.valueOf(String.valueOf(hydroInfo.get("RATES")));
                }
                if (contractInfo == null) {
                    if (pd.getString("type").equals(CodeConstants.HYDROPOWER_ELECTRIC)) {
                        if (hydroInfo.containsKey("ELETRIC_PRICE") && StringHelper.isNotEmpty(String.valueOf(hydroInfo.get("ELETRIC_PRICE")))) {
                            unitPrice = Double.valueOf(String.valueOf(hydroInfo.get("ELETRIC_PRICE")));
                        } else {
                            errcode = "03";//请先设置该电表所在楼宇默认单价
                        }
                    } else {
                        if (hydroInfo.containsKey("WATER_PRICE") && StringHelper.isNotEmpty(String.valueOf(hydroInfo.get("WATER_PRICE")))) {
                            unitPrice = Double.valueOf(String.valueOf(hydroInfo.get("WATER_PRICE")));
                        } else {
                            errcode = "04";//请先设置该水表所在楼宇默认单价
                        }
                    }
                    totalDegrees = curDegrees - lastDegrees;
                } else {
                    double firstDegrees = 0;
                    if (contractInfo.containsKey("INIT_READ") && StringHelper.isNotEmpty(String.valueOf(contractInfo.get("INIT_READ")))) {
                        firstDegrees = Double.valueOf(String.valueOf(contractInfo.get("INIT_READ")));//合同记录度数
                    }
                    if (String.valueOf(hydroInfo.get("IS_NEW")).equals("1")) {
                        totalDegrees = curDegrees - firstDegrees;//首次账单减合同度数
                        PageData hydro = new PageData();
                        hydro.put("id", hydroInfo.getString("HYDRO_ID"));
                        hydro.put("isNew", 0);
                        hydropowerService.edit(hydro);
                    } else {
                        totalDegrees = curDegrees - lastDegrees;
                    }
                    if (contractInfo.containsKey("IS_DEFAULT") && StringHelper.isNotEmpty(String.valueOf(contractInfo.get("IS_DEFAULT")))) {
                        if (String.valueOf(contractInfo.get("IS_DEFAULT")).equals("0")) {
                            if (contractInfo.containsKey("CHARGE_PRICE") && StringHelper.isNotEmpty(String.valueOf(contractInfo.get("CHARGE_PRICE")))) {
                                unitPrice = Double.valueOf(String.valueOf(contractInfo.get("CHARGE_PRICE")));
                            } else {
                                errcode = "05";//物业合同水电费抄表收费自定义时收费金额为空
                            }
                        } else {
                            if (contractInfo.containsKey("PRICE_TYPE") && StringHelper.isNotEmpty(String.valueOf(contractInfo.get("PRICE_TYPE")))) {
                                unitPrice = Double.valueOf(String.valueOf(contractInfo.get("PRICE_TYPE")));
                            } else {
                                errcode = "06";//物业合同水电费抄表收费选择时收费金额为空
                            }
                            if (contractInfo.containsKey("PROPERTY_FPZB") && StringHelper.isNotEmpty(String.valueOf(contractInfo.get("PROPERTY_FPZB")))) {
                                proportion = Double.valueOf(String.valueOf(contractInfo.get("PROPERTY_FPZB")));
                            }
                        }
                    } else {
                        errcode = "07";//该水电表对应物业合同水电费是否自定义收费存储有误
                    }
                }
            } else {
                errcode = "08";
            }
            if (errcode.equals("00")) {
                if (rates != 0) {
                    totalDegrees *= rates;
                }
                totalPrice = totalDegrees * unitPrice;
               /* if (proportion != 0) {
                    double publicPrice = totalPrice * proportion / 100;
                    PageData billPullic = billService.getBillNew(contractInfo, hydroInfo, publicPrice, this.get32UUID(), this.getSessionUser().getUSER_ID(), billType, unitPrice, "0", state);
                    billPullic.put("HYDROPOWER_ID", hID);
                    billService.save(billPullic);
                    double privatePropotion = 1 - proportion / 100;
                    if (privatePropotion > 0) {
                        double privatePrice = totalPrice * privatePropotion;
                        PageData privatePullic = billService.getBillNew(contractInfo, hydroInfo, privatePrice, this.get32UUID(), this.getSessionUser().getUSER_ID(), billType, unitPrice, "1", state);
                        privatePullic.put("HYDROPOWER_ID", hID);
                        billService.save(privatePullic);
                    }
                } else {
                    PageData bill = billService.getBillNew(contractInfo, hydroInfo, totalPrice, this.get32UUID(), this.getSessionUser().getUSER_ID(), billType, unitPrice, "1", state);
                    bill.put("HYDROPOWER_ID", hID);
                    billService.save(bill);
                }*/
                PageData detail = new PageData();
                detail.put("id", hydroInfo.getString("DETAIL_ID"));
                detail.put("isBill", 1);
                hydropowerDetailService.edit(detail);
                resultPd.put("errmsg", "操作成功");
            } else if (errcode.equals("01")) {
                resultPd.put("errmsg", "该水电表上次读数为空");
            } else if (errcode.equals("02")) {
                resultPd.put("errmsg", "该水电表本次读数为空");
            } else if (errcode.equals("03")) {
                resultPd.put("errmsg", "请先设置该电表所在楼宇默认单价");
            } else if (errcode.equals("04")) {
                resultPd.put("errmsg", "请先设置该水表所在楼宇默认单价");
            } else if (errcode.equals("05")) {
                resultPd.put("errmsg", "物业合同水电费抄表收费自定义时收费金额为空");
            } else if (errcode.equals("06")) {
                resultPd.put("errmsg", "物业合同水电费抄表收费选择时收费金额为空");
            } else if (errcode.equals("07")) {
                resultPd.put("errmsg", "物业合同抄表收费方式为空");
            } else if (errcode.equals("08")) {
                resultPd.put("errmsg", "请先设置该电表本次记录日期");
            }
            resultPd.put("errcode", errcode);
        } catch (Exception e) {
            resultPd.put("errcode", "09");
            resultPd.put("errmsg", "操作失败");
            logger.error(e.toString(), e);
        }
        return resultPd;
    }

    /**
     * 批量生成账单
     *
     * @return
     */
    @RequestMapping(value = "/genbillmanynew")
    @ResponseBody
    public Object genBillManyNew() {
        logBefore(logger, "批量生成账单");
        PageData pd = new PageData();
        PageData resultPd = new PageData();
        try {
            pd = this.getPageData();
            String data = pd.getString("data");
            String[] ds = data.split("#");
            String errcode = "00";
            String hID = null;
            if (ds.length > 0) {
                for (String info : ds) {
                    String[] is = info.split(",");
                    String eid = is[0];
                    String type = is[1];
                    String time = is[2];
                    int year = 0;
                    int month = 0;
                    if (StringHelper.isNotEmpty(time)) {
                        String[] dates = time.split("-");
                        year = Integer.parseInt(dates[0]);
                        month = Integer.parseInt(dates[1]);
                    } else {
                        errcode = "08";
                    }
                    pd.put("year", year);
                    pd.put("month", month);
                    String billType = "";
                    if (type.equals(CodeConstants.HYDROPOWER_ELECTRIC)) {
                        billType = CodeConstants.PROPERTY_ELECTRIC;
                    } else {
                        billType = CodeConstants.PROPERTY_WATER;
                    }
                    pd.put("state", CodeConstants.CONTRACT_UNDER);
                    pd.put("eid", eid);
                    pd.put("type", type);
                    //查询有效合同信息
                    PageData contractInfo = hydropowerDetailService.findContractDatas(pd);
                    //查询电表信息
                    PageData hydroInfo = hydropowerDetailService.findHydroInfo(pd);
                    hID = hydroInfo.getString("HYDRO_ID");
                    String state = CodeConstants.PAYMENT_WAIT;
                    if (hydroInfo.containsKey("IS_CARD") && hydroInfo.get("IS_CARD").equals("1")) {
                        state = CodeConstants.PAYMENT_END;
                    }
                    double totalPrice = 0;//水电总费用
                    double unitPrice = 0;//水电总价
                    double totalDegrees = 0;//水电总度数
                    double lastDegrees = 0;//上月底数
                    double curDegrees = 0;//本月度数
                    double rates = 0;//电表倍率
                    double proportion = 0;//发票占比
                    if (hydroInfo.containsKey("LAST_DEGREES") && StringHelper.isNotEmpty(String.valueOf(hydroInfo.get("LAST_DEGREES")))) {
                        lastDegrees = Double.valueOf(String.valueOf(hydroInfo.get("LAST_DEGREES")));
                    } else {
                        errcode = "01";//该水电表上次读数为空
                    }
                    if (hydroInfo.containsKey("CUR_DEGREES") && StringHelper.isNotEmpty(String.valueOf(hydroInfo.get("CUR_DEGREES")))) {
                        curDegrees = Double.valueOf(String.valueOf(hydroInfo.get("CUR_DEGREES")));
                    } else {
                        errcode = "02";//该水电表本次读数为空
                    }
                    if (hydroInfo.containsKey("RATES") && StringHelper.isNotEmpty(String.valueOf(hydroInfo.get("RATES")))) {
                        rates = Double.valueOf(String.valueOf(hydroInfo.get("RATES")));
                    }
                    if (contractInfo == null) {
                        if (pd.getString("type").equals(CodeConstants.HYDROPOWER_ELECTRIC)) {
                            if (hydroInfo.containsKey("ELETRIC_PRICE") && StringHelper.isNotEmpty(String.valueOf(hydroInfo.get("ELETRIC_PRICE")))) {
                                unitPrice = Double.valueOf(String.valueOf(hydroInfo.get("ELETRIC_PRICE")));
                            } else {
                                errcode = "03";//请先设置该电表所在楼宇默认单价
                            }
                        } else {
                            if (hydroInfo.containsKey("WATER_PRICE") && StringHelper.isNotEmpty(String.valueOf(hydroInfo.get("WATER_PRICE")))) {
                                unitPrice = Double.valueOf(String.valueOf(hydroInfo.get("WATER_PRICE")));
                            } else {
                                errcode = "04";//请先设置该水表所在楼宇默认单价
                            }
                        }
                        totalDegrees = curDegrees - lastDegrees;
                    } else {
                        double firstDegrees = 0;
                        if (contractInfo.containsKey("INIT_READ") && StringHelper.isNotEmpty(String.valueOf(contractInfo.get("INIT_READ")))) {
                            firstDegrees = Double.valueOf(String.valueOf(contractInfo.get("INIT_READ")));//合同记录度数
                        }
                        if (String.valueOf(hydroInfo.get("IS_NEW")).equals("1")) {
                            totalDegrees = curDegrees - firstDegrees;//首次账单减合同度数
                            PageData hydro = new PageData();
                            hydro.put("id", hydroInfo.getString("HYDRO_ID"));
                            hydro.put("isNew", 0);
                            hydropowerService.edit(hydro);
                        } else {
                            totalDegrees = curDegrees - lastDegrees;
                        }
                        if (contractInfo.containsKey("IS_DEFAULT") && StringHelper.isNotEmpty(String.valueOf(contractInfo.get("IS_DEFAULT")))) {
                            if (String.valueOf(contractInfo.get("IS_DEFAULT")).equals("0")) {
                                if (contractInfo.containsKey("CHARGE_PRICE") && StringHelper.isNotEmpty(String.valueOf(contractInfo.get("CHARGE_PRICE")))) {
                                    unitPrice = Double.valueOf(String.valueOf(contractInfo.get("CHARGE_PRICE")));
                                } else {
                                    errcode = "05";//物业合同水电费抄表收费自定义时收费金额为空
                                }
                            } else {
                                if (contractInfo.containsKey("PRICE_TYPE") && StringHelper.isNotEmpty(String.valueOf(contractInfo.get("PRICE_TYPE")))) {
                                    unitPrice = Double.valueOf(String.valueOf(contractInfo.get("PRICE_TYPE")));
                                } else {
                                    errcode = "06";//物业合同水电费抄表收费选择时收费金额为空
                                }
                                if (contractInfo.containsKey("PROPERTY_FPZB") && StringHelper.isNotEmpty(String.valueOf(contractInfo.get("PROPERTY_FPZB")))) {
                                    proportion = Double.valueOf(String.valueOf(contractInfo.get("PROPERTY_FPZB")));
                                }
                            }
                        } else {
                            errcode = "07";//该水电表对应物业合同水电费是否自定义收费存储有误
                        }
                    }

                    if (errcode.equals("00")) {
                        if (rates != 0) {
                            totalDegrees *= rates;
                        }
                        totalPrice = totalDegrees * unitPrice;
                       /* if (proportion != 0) {
                            double publicPrice = totalPrice * proportion / 100;
                            PageData billPullic = billService.getBillNew(contractInfo, hydroInfo, publicPrice, this.get32UUID(), this.getSessionUser().getUSER_ID(), billType, unitPrice, "0", state);
                            billPullic.put("HYDROPOWER_ID", hID);
                            billService.save(billPullic);
                            double privatePropotion = 1 - proportion / 100;
                            if (privatePropotion > 0) {
                                double privatePrice = totalPrice * privatePropotion;
                                PageData privatePullic = billService.getBillNew(contractInfo, hydroInfo, privatePrice, this.get32UUID(), this.getSessionUser().getUSER_ID(), billType, unitPrice, "1", state);
                                privatePullic.put("HYDROPOWER_ID", hID);
                                billService.save(privatePullic);
                            }
                        } else {
                            PageData bill = billService.getBillNew(contractInfo, hydroInfo, totalPrice, this.get32UUID(), this.getSessionUser().getUSER_ID(), billType, unitPrice, "1", state);
                            bill.put("HYDROPOWER_ID", hID);
                            billService.save(bill);
                        }*/
                        PageData detail = new PageData();
                        detail.put("id", hydroInfo.getString("DETAIL_ID"));
                        detail.put("isBill", 1);
                        hydropowerDetailService.edit(detail);
                    }
                }
            }
            resultPd.put("errcode", "00");
            resultPd.put("errmsg", "操作成功");
        } catch (Exception e) {
            resultPd.put("errcode", "01");
            resultPd.put("errmsg", "操作失败");
            logger.error(e.toString(), e);
        }
        return resultPd;
    }

    /**
     * 水电表 详情页列表
     */
    @RequestMapping(value = "/hydropowerInfoList")
    @ResponseBody
    public Object hydropowerInfoList(Page page) {
        logBefore(logger, "详情页列表");
        PageData pd = new PageData();
        String datalayui = null;
        try {
            pd = this.getPageData();
            getPData(page, pd);
            page.setPd(pd);
            List<PageData> varList = hydropowerDetailService.hydropowerInfoList(page);    //列出Hydropower_detail列表
            datalayui = getLData(page, varList);//必填 2参
        } catch (Exception e) {
            logger.error(e.toString(), e);
        }
        return datalayui;
    }

    /**
     * 电表详情页 详情数据
     */
    @RequestMapping(value = "/find_info_by_EID")
    @ResponseBody
    public Object find_info_by_EID() {
        logBefore(logger, "电表详情页");
        PageData pd = new PageData();
        PageData resultPd = new PageData();
        try {
            pd = this.getPageData();
            List<PageData> list = hydropowerDetailService.find_info_by_EID(pd);
            resultPd.put("data", list);
            resultPd.put("errcode", "00");
            resultPd.put("errmsg", "获取成功");
        } catch (Exception e) {
            resultPd.put("errcode", "01");
            resultPd.put("errmsg", "获取失败");
            logger.error(e.toString(), e);
        }
        return resultPd;
    }

    /**
     * 总量 分表  详情数据
     */
    @RequestMapping(value = "/amount_difference")
    @ResponseBody
    public Object amount_difference() {
        logBefore(logger, "总量 分表  详情数据");
        PageData pd = new PageData();
        PageData resultPd = new PageData();
        try {
            pd = this.getPageData();
            pd.put("S_DATE", pd.getString("S_DATE"));
            pd.put("E_DATE", pd.getString("E_DATE"));
            List<PageData> total_list = hydropowerDetailService.amount_difference(pd);
            resultPd.put("data", total_list.get(0));//存放当前总表的读数
            //子表数据
            PageData childernPd = new PageData();
            childernPd.put("PID", pd.getString("ID"));//ID 是主表id  传给pid 求子表数据 多个子表 依次循环
            childernPd.put("S_DATE", pd.getString("S_DATE"));//ID 是主表id  传给pid 求子表数据 多个子表 依次循环
            childernPd.put("E_DATE", pd.getString("E_DATE"));//ID 是主表id  传给pid 求子表数据 多个子表 依次循环
            List<PageData> childern_list = hydropowerDetailService.childern_difference(childernPd);
            resultPd.put("childern_list", childern_list.get(0));//存放当前总表的读数
            resultPd.put("errcode", "00");
            resultPd.put("errmsg", "获取成功");
        } catch (Exception e) {
            resultPd.put("errcode", "01");
            resultPd.put("errmsg", "获取失败");
            logger.error(e.toString(), e);
        }
        return resultPd;
    }

    //得到所有的子节点
    private void find_childern(PageData pd) throws Exception {
        PageData childernPd = new PageData();
        childernPd.put("PID", pd.getString("ID"));//ID 是主表id  传给pid 求子表数据 多个子表 依次循环
        List<PageData> childerns = new ArrayList<PageData>();
        List<PageData> childern_list = hydropowerDetailService.childern_difference(childernPd);
        childerns.addAll(childern_list);//把当前子数据加到当前位置
        for (PageData d : childern_list) {
            find_childern(pd);
        }
    }

    /**
     * 删除抄表记录
     */
    @RequestMapping(value = "/del_GenerateBills")
    @ResponseBody
    public Object del_GenerateBills() {
        logBefore(logger, "删除抄表记录");
        PageData pd = new PageData();
        PageData resultPd = new PageData();
        try {
            pd = this.getPageData();
            hydropowerDetailService.del_GenerateBills(pd);
            resultPd.put("errcode", "00");
            resultPd.put("errmsg", "删除成功");
        } catch (Exception e) {
            resultPd.put("errcode", "01");
            resultPd.put("errmsg", "删除失败");
            logger.error(e.toString(), e);
        }
        return resultPd;
    }

    /**
     * 生成账单新
     *
     * @return
     */
    @RequestMapping(value = "/GenerateBills")
    @ResponseBody
    public Object GenerateBills() {
        logBefore(logger, "生成账单");
        PageData pd = new PageData();
        PageData resultPd = new PageData();
        String errcode = "00";
        try {
            pd = this.getPageData();
            String billType = "";
            String hy_Type = "";
            if (pd.getString("type").equals(CodeConstants.HYDROPOWER_ELECTRIC)) {
                billType = CodeConstants.PROPERTY_ELECTRIC;
                hy_Type = "电费";
            } else {
                billType = CodeConstants.PROPERTY_WATER;
                hy_Type = "水费";
            }
            pd.put("state", CodeConstants.CONTRACT_UNDER);
            String time = pd.getString("time");
            int year = 0;
            int month = 0;
            int day = 0;
            double totalPrice = 0;//水电总费用
            double unitPrice = 0;//水电总价
            double totalDegrees = 0;//水电总度数
            double lastDegrees = 0;//上月底数
            double curDegrees = 0;//本月度数
            double rates = 0;//电表倍率
            double proportion = 0;//占比
            List<PageData> contractInfo = new ArrayList<>();//一个表 多个合同
            List<PageData> hydroInfo = new ArrayList<>();//获取当前水电表的读数 多个
            String state = CodeConstants.PAYMENT_WAIT;
            String hID = null;
            if (StringHelper.isNotEmpty(time)) {
                String[] dates = time.split("-");
                year = Integer.parseInt(dates[0]);
                month = Integer.parseInt(dates[1]);
                pd.put("year", year);
                pd.put("month", month);
                //查询有效合同信息
                pd.put("CODE", hy_Type);
                contractInfo = hydropowerDetailService.findContractDatas_new(pd);

                //查询电表信息
                hydroInfo = hydropowerDetailService.findHydroInfo_new(pd);
                hID = hydroInfo.get(0).getString("HYDRO_ID");
                //是否插卡
                if (hydroInfo.get(0).containsKey("IS_CARD") && String.valueOf(hydroInfo.get(0).get("IS_CARD")).equals("1")) {
                    state = CodeConstants.PAYMENT_END;
                }
                //todo 判断上次的读数 和本次的读数 取出最新的2条记录  1当前本次读数 2读取上次读数 小当前日期的一个月的读数
                curDegrees = Double.parseDouble(pd.get("CUR_DEGREES").toString());//1当前本次读数
                //读取上期读数 小于当前月的
                List<PageData> lastList = new ArrayList<>();//获取当前水电表的读数 多个
                PageData lastpd = new PageData();
                lastpd.put("eid", pd.getString("eid"));
                lastpd.put("year", year);
                lastpd.put("month", month);
                lastpd.put("CUR_DATE", time);
                lastList = hydropowerDetailService.findHydroInfo_last(lastpd);
                if (lastList.size() > 0) {
                    lastDegrees = Double.parseDouble(lastList.get(0).get("CUR_DEGREES").toString());
                }

                //todo 假如是手选 的2个日期
                if (pd.containsKey("data")) {
                    List<PageData> list_select = (List<PageData>) JSONArray.toList(JSONArray.fromObject(pd.get("data").toString()), PageData.class);
                    curDegrees = Double.parseDouble(list_select.get(0).get("CUR_DEGREES").toString());
                    lastDegrees = Double.parseDouble(list_select.get(1).get("CUR_DEGREES").toString());
                }
                //倍率
                if (hydroInfo.get(0).containsKey("RATES") && StringHelper.isNotEmpty(String.valueOf(hydroInfo.get(0).get("RATES")))) {
                    rates = Double.valueOf(String.valueOf(hydroInfo.get(0).get("RATES")));
                }
                //如果没有合同
                if (contractInfo.size() == 0) {
                    if (pd.getString("type").equals(CodeConstants.HYDROPOWER_ELECTRIC)) {
                        if (hydroInfo.get(0).containsKey("ELETRIC_PRICE") && StringHelper.isNotEmpty(String.valueOf(hydroInfo.get(0).get("ELETRIC_PRICE")))) {
                            unitPrice = Double.valueOf(String.valueOf(hydroInfo.get(0).get("ELETRIC_PRICE")));
                        } else {
                            errcode = "03";//请先设置该电表所在楼宇默认单价
                        }
                    } else {
                        if (hydroInfo.get(0).containsKey("WATER_PRICE") && StringHelper.isNotEmpty(String.valueOf(hydroInfo.get(0).get("WATER_PRICE")))) {
                            unitPrice = Double.valueOf(String.valueOf(hydroInfo.get(0).get("WATER_PRICE")));
                        } else {
                            errcode = "04";//请先设置该水表所在楼宇默认单价
                        }
                    }
                    totalDegrees = curDegrees - lastDegrees;
                    totalPrice = totalDegrees * unitPrice;
                } else {
                    //有合同信息 l楼宇单价
                    double firstDegrees = 0;
                    totalDegrees = curDegrees - lastDegrees;
                }
            }
            //状态为00 才走
            if (errcode.equals("00")) {
                if (rates != 0) {
                    totalDegrees *= rates;//倍率
                }
                if (contractInfo.size() > 0) {
                    for (PageData contractInfos : contractInfo) {
                        //是抄表收费才生成账单
                        if (StringHelper.isNotEmpty(contractInfos.getString("CHARGE_TYPE")) && contractInfos.getString("CHARGE_TYPE").equals("1")) {
                            if (contractInfos.containsKey("IS_DEFAULT") && StringHelper.isNotEmpty(String.valueOf(contractInfos.get("IS_DEFAULT")))) {
                                if (String.valueOf(contractInfos.get("IS_DEFAULT")).equals("0")) {
                                    if (contractInfos.containsKey("CHARGE_PRICE") && StringHelper.isNotEmpty(String.valueOf(contractInfos.get("CHARGE_PRICE")))) {
                                        unitPrice = Double.valueOf(String.valueOf(contractInfos.get("CHARGE_PRICE")));
                                    } else {
                                        errcode = "05";//物业合同水电费抄表收费自定义时收费金额为空
                                    }
                                } else {
                                    //判读默认价的水电费 单价
                                    if (contractInfos.containsKey("PRICE_TYPE") || contractInfos.containsKey("PRICE_TYPE")) {
                                        if ("电费".equals(hy_Type)) {
                                            unitPrice = Double.valueOf(String.valueOf(contractInfos.get("PRICE_TYPE")));
                                        } else {
                                            unitPrice = Double.valueOf(String.valueOf(contractInfos.get("PRICE_TYPE")));
                                        }

                                    } else {
                                        errcode = "06";//物业合同水电费抄表收费选择时收费金额为空
                                    }
                                }
                            } else {
                                errcode = "07";//该水电表对应物业合同水电费是否自定义收费存储有误
                            }
                            totalPrice = totalDegrees * unitPrice;
                            //房间占比
                            if (pd.getString("type").equals(CodeConstants.HYDROPOWER_ELECTRIC)) {
                                if (contractInfos.containsKey("E_BL") && StringHelper.isNotEmpty(String.valueOf(contractInfos.get("E_BL")))) {
                                    proportion = Double.valueOf(String.valueOf(contractInfos.get("E_BL")));
                                } else {
                                    proportion = 100;
                                }
                            } else {//房间占比
                                if (contractInfos.containsKey("W_BL") && StringHelper.isNotEmpty(String.valueOf(contractInfos.get("W_BL")))) {
                                    proportion = Double.valueOf(String.valueOf(contractInfos.get("W_BL")));
                                } else {
                                    proportion = 100;
                                }
                            }
                            double publicPrice = totalPrice * proportion / 100;
                            //公户私户 占比  100%公户
                            double fp_proportion = 0;
                            if (contractInfos.containsKey("PROPERTY_FPZB") && StringHelper.isNotEmpty(String.valueOf(contractInfos.get("PROPERTY_FPZB")))) {
                                fp_proportion = Double.valueOf(String.valueOf(contractInfos.get("PROPERTY_FPZB")));
                            }
                            // 滞纳金 占比
                            double  LATE_FEE= 0;
                            if (contractInfos.containsKey("LATE_FEE") && StringHelper.isNotEmpty(String.valueOf(contractInfos.get("LATE_FEE")))) {
                                LATE_FEE = Double.valueOf(String.valueOf(contractInfos.get("LATE_FEE")));
                            }
                            if(LATE_FEE!=0){
                                LATE_FEE=publicPrice*LATE_FEE/ 100; //滞纳金
                            }
                            if (fp_proportion != 0) {
                                publicPrice = fp_proportion * publicPrice / 100;
                                PageData privatePullic = billService.getBillNew(contractInfos, hydroInfo, publicPrice, this.get32UUID(), this.getSessionUser().getUSER_ID(), billType, unitPrice, "0", state,LATE_FEE);
                                privatePullic.put("HYDROPOWER_ID", hID);
                                billService.save(privatePullic);
                                double privatePropotion = 1 - fp_proportion / 100;
                                if (privatePropotion > 0) {//如果公户不为100% 剩下的都是私户
                                    double privatePrice = totalPrice * privatePropotion;
                                    PageData privatePullic1 = billService.getBillNew(contractInfos, hydroInfo, privatePrice, this.get32UUID(), this.getSessionUser().getUSER_ID(), billType, unitPrice, "1", state,LATE_FEE);
                                    privatePullic1.put("HYDROPOWER_ID", hID);
                                    billService.save(privatePullic1);
                                }
                            } else {
                                PageData bill = billService.getBillNew(contractInfos, hydroInfo, publicPrice, this.get32UUID(), this.getSessionUser().getUSER_ID(), billType, unitPrice, "1", state,LATE_FEE);
                                bill.put("HYDROPOWER_ID", hID);
                                billService.save(bill);
                            }
                        }else {
                            errcode="01";
                        }
                    }
                } else {
                    PageData bill = new PageData();
                    String tenantId = "";
                    String contractId = "";
                    String days = "0";
                    bill.put("ID", this.get32UUID());
                    bill.put("CREATE_ID", this.getSessionUser().getUSER_ID());
                    bill.put("CREATE_DATE", DateUtil.getTime());
                    bill.put("UPDATE_DATE", DateUtil.getTime());
                    bill.put("SORT", 0);
                    bill.put("IS_DEL", 0);
                    bill.put("TENANT_ID", tenantId);
                    bill.put("CONTRACT_ID", contractId);
                    bill.put("START_DATE", DateUtil.getLastMonthDate());
                    bill.put("PAY_DATE", days == null ? DateUtil.getDay() : DateUtil.getAfterDayDate(days));
                    bill.put("END_DATE", DateUtil.getDay());
                    bill.put("TOTAL_PRICE", totalPrice);
                    bill.put("BILL_TYPE", billType);//存储账单类型
                    if ("a14e6f2e190e4492badac72e9b42152e".equals(billType)) {
                        bill.put("BILL_NAME", "水费");//存储账单类型
                    } else if ("ddcf9b35bbe04cd88834e21c1fd74f32".equals(billType)) {
                        bill.put("BILL_NAME", "电费");//存储账单类型
                    }
                    //bill.put("BILL_NAME",billType);//存储账单类型
                    bill.put("STATE", state);
                    bill.put("HYDROPOWER_ID", hID);
                    bill.put("PRICE_TYPE", 0);//收入
                    bill.put("UNIT_PRICE", unitPrice);
                    bill.put("ALLOT_BILL", "1");
                    bill.put("CHECK_STATE", "01");//水电表的生成 都需要审核
                    bill.put("MAKE_BILL_TYPE", 1);
                    billService.save(bill);
                }
                PageData detail = new PageData();
                detail.put("id", pd.getString("id"));
                detail.put("isBill", 1);
                hydropowerDetailService.edit(detail);
                resultPd.put("errmsg", "操作成功");
            } else if (errcode.equals("01")) {
                resultPd.put("errmsg", "该水电表上次读数为空");
            } else if (errcode.equals("02")) {
                resultPd.put("errmsg", "该水电表本次读数为空");
            } else if (errcode.equals("03")) {
                resultPd.put("errmsg", "请先设置该电表所在楼宇默认单价");
            } else if (errcode.equals("04")) {
                resultPd.put("errmsg", "请先设置该水表所在楼宇默认单价");
            } else if (errcode.equals("05")) {
                resultPd.put("errmsg", "物业合同水电费抄表收费自定义时收费金额为空");
            } else if (errcode.equals("06")) {
                resultPd.put("errmsg", "物业合同水电费抄表收费选择时收费金额为空");
            } else if (errcode.equals("07")) {
                resultPd.put("errmsg", "物业合同抄表收费方式为空");
            } else if (errcode.equals("08")) {
                resultPd.put("errmsg", "请先设置该电表本次记录日期");
            } else if (errcode.equals("08")) {
                resultPd.put("errmsg", "该表为固定价格,无需生成账单");
            }
            resultPd.put("errcode", errcode);
        } catch (Exception e) {
            resultPd.put("errcode", "09");
            resultPd.put("errmsg", "操作失败");
            logger.error(e.toString(), e);
        }
        return resultPd;
    }
}
